package fpc.controls.buttons;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.MouseEvent;
import fpc.AComponent;
import fpc.IAutoSizable;
import fpc.styles.skins.IScaledButtonSkin;
import fpc.styles.skins.NullScaledButtonSkin;
import fpc.styles.skins.SkinType;
import fpc.styles.Style;
import fpc.utils.ClassFactory;
import fpc.utils.IFactory;

class Button extends AButton, implements IAutoSizable
{	
	public function new(label:String = "", clickHandler:IButton->Dynamic = null)
	{
		_autoSize = false;
		
		super();
		
		width = 100;
		height = 20;
		
		if(clickHandler != null)
		{
			heraldClick.add(clickHandler);
		}
		this.label = label;
	}
	
	override function createBaseObject():DisplayObject
	{
		_sprite = new Sprite();
		return _sprite;
	}
	
	override function render()
	{
		super.render();
		_scaledSkin.width = Std.int(_width);
		_scaledSkin.height = Std.int(_height);
		_scaledSkin.update();
	}
	
	public var label(getLabel, setLabel):String;
	var _labelText:String;
	function setLabel(value:String)
	{
		_labelText = value;
		_scaledSkin.text = value;
		_scaledSkin.calculateMinSize();
		tryResize(_width, _height);
		orderRender();
		return value;
	}
	function getLabel()
	{
		return _labelText;
	}
	
	// LAYOUT
	
	public var autoSize(getAutoSize, setAutoSize):Bool;
	var _autoSize:Bool;
	function getAutoSize()
	{
		return _autoSize;
	}
	function setAutoSize(value:Bool)
	{
		_autoSize = value;
		if (value)
		{
			tryResize(_width, _height);
		}
		return _autoSize;
	}
	
	override function tryResize(newWidth:Float, newHeight:Float)
	{
		if (_autoSize)
		{
			newWidth = _scaledSkin.minWidth;
			newHeight = _scaledSkin.minHeight;
		}
		else
		{
			if (newWidth < _scaledSkin.minWidth)
			{
				newWidth = _scaledSkin.minWidth;
			}
			if (newHeight < _scaledSkin.minHeight)
			{
				newHeight = _scaledSkin.minHeight;
			}
		}
		if (Std.int(newWidth) != Std.int(_width) || Std.int(newHeight) != Std.int(_height))
		{
			_width = newWidth;
			_height = newHeight;
			orderRender();
			heraldResize.dispatch(this);
		}
		else
		{
			_width = newWidth;
			_height = newHeight;
		}
	}
	
	override function setWidth(value)
	{
		if (Math.isNaN(value))
		{
			return _width;
		}
		tryResize(value, _height);
		return _width;
	}
	
	override function setHeight(value)
	{
		if (Math.isNaN(value))
		{
			return _height;
		}
		tryResize(_width, value);
		return _height;
	}
	
	// SKINS
	
	var _scaledSkin:IScaledButtonSkin;
	
	var _skinFactory:IFactory<IScaledButtonSkin>;
	
	override function updateStyle()
	{
		var skinFactory = Style.findSkin(this, SKIN_TYPE_BUTTON);
		if (skinFactory != _skinFactory)
		{
			_skinFactory = skinFactory;
			
			var skin = skinFactory.create();
			skin.text = _labelText;
			skin.calculateMinSize();
			
			_scaledSkin = skin;
			applyButtonSkin(skin);
		}
	}
	
	public static var SKIN_TYPE_BUTTON = new SkinType(
		new ClassFactory<IScaledButtonSkin>(NullScaledButtonSkin)
	);
}
